<template>
  <NavbarComponent />
  <div class="search-page">
    <h1>搜索结果：{{ searchKeyword }}</h1>
    
    <!-- 商品展示区 -->
    <section class="products">
      <div v-if="loading" class="loading">
        <i class="el-icon-loading"></i> 正在搜索中...
      </div>
      <div v-else-if="error" class="error">{{ error }}</div>
      <div v-else>
        <div class="product-grid">
          <div
            class="product-card"
            v-for="product in searchResults"
            :key="product.productId"
            @click="openProductDetail(product.productId)"
          >
            <div class="image-container">
              <img :src="'http://localhost:8092' + product.images" alt="商品图片" />
            </div>
            <div class="card-content">
              <h3 class="product-title">{{ product.productName }}</h3>
              <p class="product-description">{{ product.introduction }}</p>
              <div class="product-footer">
                <span class="product-price">¥{{ product.price }}</span>
                <span class="product-wants">{{ product.wants || 0 }}人想要</span>
              </div>
            </div>
          </div>
        </div>
        <div v-if="searchResults.length === 0" class="no-data">
          没有找到与 "{{ searchKeyword }}" 相关的商品
        </div>
      </div>
    </section>
    <RightSidebar />
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { useRoute ,useRouter} from 'vue-router'
import axios from 'axios'
import NavbarComponent from '@/components/components/NavbarComponent.vue'
import RightSidebar from '@/components/components/RightSidebar.vue'

const route = useRoute()
const searchKeyword = ref('')
const searchResults = ref([])
const loading = ref(false)
const error = ref('')
const router = useRouter()
// 执行搜索
const performSearch = async (keyword) => {
  try {
    loading.value = true
    error.value = ''
    const response = await axios.get('http://localhost:8092/product/search', {
      params: { keyword }
    })

    if (response.data.code === 200) {
      searchResults.value = response.data.data || []
    } else {
      error.value = response.data.msg || '搜索失败'
      searchResults.value = []
    }
  } catch (err) {
    error.value = `搜索请求失败：${err.message}`
    console.error('搜索出错:', err)
    searchResults.value = []
  } finally {
    loading.value = false
  }
}

// 打开商品详情
const openProductDetail = (productId) => {
  const routeData = router.resolve({
    path: '/product',
    query: { productId }
  })
  window.open(routeData.href, '_blank')
}

// 监听路由变化
watch(
  () => route.query.keyword,
  (newVal) => {
    if (newVal) {
      searchKeyword.value = newVal
      performSearch(newVal)
    }
  },
  { immediate: true }
)

// 初始加载
onMounted(() => {
  searchKeyword.value = route.query.keyword || ''
  if (searchKeyword.value) {
    performSearch(searchKeyword.value)
  }
})
</script>

<style scoped>
.search-page {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

h1 {
  color: #2c3e50;
  border-bottom: 2px solid #3498db;
  padding-bottom: 10px;
  margin-bottom: 30px;
  font-size: 1.5em;
}

.loading {
  text-align: center;
  padding: 40px 0;
  color: #666;
}

.loading .el-icon-loading {
  font-size: 24px;
  margin-right: 10px;
  animation: rotating 2s linear infinite;
}

.error {
  color: #e74c3c;
  text-align: center;
  padding: 40px 0;
}

.no-data {
  text-align: center;
  color: #95a5a6;
  padding: 40px 0;
  font-size: 16px;
}

/* 复用商品卡片样式 */
.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 30px;
  padding: 20px;
}

.product-card {
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  overflow: hidden;
  cursor: pointer;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.image-container {
  width: 100%;
  height: 220px;
  overflow: hidden;
  position: relative;
}

.image-container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.product-card:hover img {
  transform: scale(1.05);
}

.card-content {
  padding: 18px;
}

.product-title {
  font-size: 16px;
  color: #34495e;
  margin: 0 0 12px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}

.product-description {
  font-size: 14px;
  color: #7f8c8d;
  line-height: 1.5;
  margin: 0 0 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.product-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.product-price {
  color: #ff5500;
  font-size: 18px;
  font-weight: bold;
}

.product-wants {
  color: #95a5a6;
  font-size: 13px;
}

@keyframes rotating {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

@media (max-width: 768px) {
  .product-grid {
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 20px;
    padding: 10px;
  }
  
  .image-container {
    height: 200px;
  }
}
</style>